<template>
    <div>
        <div class="card card-custom gutter-b example example-compact" id="tableBody">
            <div class="card-header">
                <div class="card-title">
                    <span class="card-icon">              
                    <i class="text-dark-50 flaticon-search-magnifier-interface-symbol"></i>
                    </span>
                    <h3 class="card-label"> 查询区域 </h3>
                </div>
                <div class="card-toolbar">
                    <div class="example-tools justify-content-center">
                    <!-- 
                        <span data-toggle="tooltip" class="example-toggle"></span>
                        <span data-toggle="tooltip" class="example-copy"></span> 
                    -->
                    </div>
                </div>
            </div>
            <div class="card-body">
                <div class="m-form m-form--fit m-form--label-align-left m-form--group-seperator-dashed">
                    <div class="m-portlet__body">	
                        <div class="form-group m-form__group row">        
                            <label class="col-form-label">名称：</label>
                            <div class="col-lg-2">
                                <input type="text" class="form-control" v-model="searchForm.name" placeholder="请输入">
                            </div>
                            <label class="col-form-label">状态：</label>
                            <div class="col-lg-2">
                                <el-select maxlength="20" v-model="searchForm.status" placeholder="请选择">
                                    <el-option value="">请选择</el-option>
                                    <el-option
                                        v-for="item in [{value:'0',label:'正常'},{value:'1',label:'冻结'}]"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value">
                                    </el-option>
                                </el-select>
                            </div>   
                            <b-button :pressed="false" variant="btn btn-primary font-weight-bold font-size-sm py-3 px-8 mr-2 " @click="search()"><span><i
                            class="flaticon-search-magnifier-interface-symbol"></i><span>查 询</span></span></b-button>
                            <b-button :pressed="false" variant="btn btn-light font-weight-bold font-size-sm py-3 px-8 mr-2 " @click="resetAll()">
                            <span><i class="flaticon2-refresh"></i><span>重 置</span></span></b-button>                                
                            <a class="btn btn-light-success font-weight-bold font-size-sm py-3 px-8 mr-2" 
                            v-permission="{ action: '/oauthResources/add', effect: 'display' }"
                            @click="addResources">
                                <span><i class="la la-plus la-lg"></i><span>创建资源</span></span>
                            </a>
                            <a class="btn btn-light-primary font-weight-bold font-size-sm py-3 px-8 mr-2" 
                            v-permission="{ action: '/oauthResources/import', effect: 'display' }"
                            @click="importOauthResources">
                                <span><i class="fa flaticon-folder-3"></i><span> 导 入</span></span>
                            </a>  
                        </div> 
                    </div> 
                    <!-- <div class="m-portlet__foot m-portlet__no-border m-portlet__foot--fit">
                        <div class="m-form__actions m-form__actions--solid">
                            <div class="row">
                                <div class="col m--align-left">           
                                </div>
                                <div class="col m--align-right">
                                </div>
                            </div>
                        </div>
                    </div> -->
                </div>
            </div>
            <!--分页组件-->
            <el-table style="width: 100%" stripe border highlight-current-row :data="sysModeList"
                @selection-change="handleSelectionChange" 
                ref="enquiry"
                :expand-row-keys="expands" 
                :row-key="getRowKeys" 
                @expand-change="expandRow"            
                @row-dblclick = "handleRowClick">
                <el-table-column type="expand">
                    <template slot-scope="scope">
                        <ResourceTree :data="scope.row" @change="search"></ResourceTree>
                    </template>
                </el-table-column>
                <el-table-column label="序号" width="50">
                    <template slot-scope="scope">
                        {{ scope.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column align="left" prop="name" show-overflow-tooltip label="隶属平台">
                    <template slot-scope="scope">
                        <i class="menu-icon text-dark-50" :class="scope.row.sys_mode_icon">                    
                        <span></span>
                        </i>
                        <span class="menu-text"> {{scope.row.name}}</span>
                    </template>
                </el-table-column>
                <el-table-column align="left" prop="status" show-overflow-tooltip label="状态" width="150">
                    <template slot-scope="scope">
                        <div class="symbol symbol-40 symbol-circle symbol-light-primary" v-if="scope.row.status == 0">									
                            <span class="symbol-label font-size-h8">正常</span>								
                        </div>
                        <div v-else-if="scope.row.status == 1" class="symbol symbol-30 symbol-circle symbol-light-success">							
                            <span class="symbol-label font-size-h8">冻结</span>								
                        </div>
                        <div class="symbol symbol-40 symbol-circle symbol-light-danger" v-else-if="scope.row.status == 2">									
                            <span class="symbol-label font-size-h8">禁用</span>								
                        </div>
                        <div v-else class="symbol symbol-40 symbol-circle symbol-light-info">
                            <span class="symbol-label font-size-h8">---</span>		
                        </div>
                    </template>
                </el-table-column>
                <!-- <el-table-column align="center" show-overflow-tooltip prop="createTime" label="创建日期"
                    min-width="200">

                </el-table-column>
                <el-table-column align="center" prop="updateTime" label="修改日期"
                    min-width="160"></el-table-column> -->
            </el-table>
            <div class="block">
                <el-pagination
                    hide-on-single-page   
                    @size-change="handleSizeChange"        
                    @current-change="handleCurrentChange"  
                    :current-page="pageNo"                
                    :page-sizes="[5, 10, 30, 50]"          
                    :page-size="pageSize"			  
                    layout="->,total, sizes, prev, pager, next, jumper"
                    :total="totalCount">		
                </el-pagination>
            </div>
        </div>
        <OauthResourcesAdd ref="oauthResourcesAddAddRef" @change="search"></OauthResourcesAdd>
        <OauthResourcesFileUpload ref="oauthResourcesFileUploadRef" @change="search"></OauthResourcesFileUpload>
    </div>
</template>
  
<script>
import { SET_BREADCRUMB } from "@/store/breadcrumbs.module";
import Swal from "sweetalert2";
import apiUtil from "@/core/util/apiUtil.js";
import OauthResourcesAdd from "@/view/oauth/oauth-resources/oauth-resources-add.vue";
import ResourceTree  from "@/view/oauth/oauth-resources/oauth-resources-tree.vue"; 
import OauthResourcesFileUpload from "@/view/oauth/oauth-resources/oauth-resources-file.vue";
import { handleNotify, handleAlert, handleConfirm, showWating, closeWating } from "@/core/util/jehcUtil.js";
export default {
    data() {
        return {
            searchForm: {name:"",status:""},
            sysModeList: [],
            subList: [],
            expands: [],
            sels: [], //当前选框选中的值
            pageNo:1,      // 默认当前是第一页
            pageSize:5,    // 当前每页的数据是5条
            totalCount:0   // 总数默认为0
        }
    },
    components: {
        OauthResourcesAdd,
        ResourceTree,
        OauthResourcesFileUpload
    },
    mounted() {
        this.$store.dispatch(SET_BREADCRUMB, [{ title: "菜单资源" }]);
        this.initList();   // 按当前的页号和每页的数据量进行查询
    },
    methods: {
        handleSelectionChange(sels) {//获取选中的值
            this.sels = sels;
        },
        handleRowClick(row, event, column) {
            //点击行展开，enquiry为主表table的ref属性值
            this.$refs.enquiry.toggleRowExpansion(row);
        },
        initList() {
            showWating({ renderBody: "tableBody" });
            this.searchForm.usePageNo = true;//采用第几页进行分页（兼容）
            this.searchForm.pageSize = this.pageSize;//页面显示记录条数，在页面显示每页显示多少项的时候
            this.searchForm.pageNo = this.pageNo;//开始的记录序号        
            apiUtil.query(process.env.VUE_APP_OAUTH_API+"/oauthSysMode/list", this.searchForm).then(({ data }) => {
                this.sysModeList = data.data;//给结果集赋值
                this.totalCount = data.total;// 获取当前数据的总数    
                this.expandAllByCondition();
            });
        },
        getRowKeys(row) {
            return row.id;
        },
        expandRow:function(row, expandedRows) {//展开与收缩事件
            console.log(this.expands,row.id);
            let _this = this;
            if(_this.expands.indexOf(row.id) >= 0){//收缩事件                
                let newExpands=[];
                for(let i=0; i<this.expands.length; i++){
                    if(this.expands[i]!=row.id){
                        newExpands.push(this.expands[i]);
                    }
                }
                _this.expands=newExpands;
                return;
            }else{
                this.expands.push(row.id);//存放第一次展开的数据
                // this.getSubList(row);//展开事件
            }
        },
        getSubList(row) {            
            //遍历主表dataList
            this.sysModeList.forEach((item, index) => {
                // if(item.id == row.id){//判断当前展开的行      
                //     showWating({renderBody:"tableBody"});          
                //     //请求子表后台接口
                //     var params = {};
                //     apiUtil.post(process.env.VUE_APP_OAUTH_API + "/oauthAdmin/listAll?id="+item.id, params).then(({ data }) => {
                //         this.sysModeList[index].subList = data.data;//给结果集赋值
                //         //如果需要默认展开所有行，将:row-key="getRowKeys"得到的Id添加到expands数组
                //         //直接使用属性default-expand-all会使子表数据显示不出来，索性全部加到this.expands
                //         this.expands.push(item.id)
                //     });
                // }                
            })
        },
        expandAllByCondition() {           // 全部展开
            let newExpands=[];
            this.sysModeList.forEach((sysModeItem,index) => {
                this.expands.forEach((expandsItem,index) => {
                    if(expandsItem==sysModeItem.id){
                        newExpands.push(expandsItem);
                    }
                });
            });            
            this.expands = [];
            setTimeout(() => {                
                this.expands = newExpands;  
                // console.log("当前需要展开节点",newExpands);
            }, 100);
        },
        handleSizeChange(val) { // 修改每页所存数据量的值所触发的函数
            this.pageSize = val;  // 修改页的大小
            this.initList();       // 按新的pageNo和pageSize进行查询
        },
        handleCurrentChange(val) { // 修改当前页所触发的函数
            this.pageNo = val;       // 更新当前的页
            this.initList();          // 按新的pageNo和pageSize进行查询
        },
        search(){
            this.pageNo = 1;       // 更新当前的页
            this.initList();          // 按新的pageNo和pageSize进行查询
        },
        resetAll(){
            Object.assign(this.$data.searchForm, this.$options.data().searchForm);
            this.search();
        },
        addResources(){
            this.$refs.oauthResourcesAddAddRef.showModal();
        },
        importOauthResources(){//导入资源
            this.$refs.oauthResourcesFileUploadRef.showModal();
        },
        delXtMenuinfo(row,parentRow) {
            // this.$confirm('确定要移除该管理员？', "提示", { type: "warning", }).then(() => {
            //     var params = { accountId: row.accountId, id: parentRow.id };               
            //     apiUtil.delete(process.env.VUE_APP_OAUTH_API + '/oauthAdmin/delete', params).then(data => {
            //         if (data.data.success) {
            //             handleAlert("移除成功", "success", 3);
            //             this.search();
            //         } else {
            //             handleAlert("移除失败", "error", 3);
            //         }
            //     });
            // }).catch(() => { });//注意这里，这里是重点！！！;
        },
        getOauthResourcesDetail(row){//详情

        }
    }
};
</script>